// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   XmlFactory.java

package com.xml.export;

import java.io.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;
import org.jdom.*;
import org.jdom.input.JDOMParseException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

// Referenced classes of package com.xml.export:
//			Path, Datum, Masking, DataBaseInfo

public class XmlFactory extends Path
{

	List columnnames;
	Vector contents;
	private String file;
	private SAXBuilder builder;
	private Document doc;
	private Element root;
	private Element table;
	private File cfile;
	private String strmask;
	private String database;
	private String tablename;
	private String xml_file;
	private String condition;
	private String cols[];
	String data;
	Element element;
	ResultSetMetaData rsmd;

	public XmlFactory()
	{
		columnnames = null;
		contents = new Vector();
		file = null;
		builder = null;
		doc = null;
		root = null;
		table = null;
		cfile = null;
		strmask = "";
		database = "";
		tablename = "";
		xml_file = "";
		condition = "";
		cols = new String[10000];
		data = "";
		element = null;
		rsmd = null;
	}

	public XmlFactory(String filename, String database, String tablename, String cols[], String xml_file, String condition)
	{
		columnnames = null;
		contents = new Vector();
		file = null;
		builder = null;
		doc = null;
		root = null;
		table = null;
		cfile = null;
		strmask = "";
		this.database = "";
		this.tablename = "";
		this.xml_file = "";
		this.condition = "";
		this.cols = new String[10000];
		data = "";
		element = null;
		rsmd = null;
		this.database = database;
		this.tablename = tablename;
		this.cols = cols;
		this.xml_file = xml_file;
		this.condition = condition;
		try
		{
			file = (new StringBuilder(String.valueOf(getPath()))).append("/").append(filename).toString();
			try
			{
				FileWriter fw = new FileWriter(file);
				fw.write("<table>");
				fw.write("</table>");
				fw.flush();
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}
			builder = new SAXBuilder();
			doc = builder.build(new File(file));
		}
		catch (JDOMParseException jpex)
		{
			try
			{
				FileWriter fw = new FileWriter(file);
				fw.write("<table>");
				fw.write("</table>");
				fw.flush();
			}
			catch (Exception exception) { }
			jpex.printStackTrace();
		}
		catch (JDOMException je)
		{
			je.printStackTrace();
		}
		catch (IOException ie)
		{
			ie.printStackTrace();
		}
	}

	public void setMask(String strmask)
	{
		this.strmask = strmask;
	}

	public void makeElement()
		throws Exception
	{
		Datum datum = new Datum(database, tablename, condition);
		Masking mask = new Masking();
		mask.init(xml_file);
		rsmd = datum.dbif.rs.getMetaData();
		int columnnumbers = rsmd.getColumnCount();
		columnnames = mask.getColumnNicks(tablename);
		table = new Element("table");
		doc.setRootElement(table);
		root = doc.getRootElement();
		while (datum.dbif.rs.next()) 
		{
			for (int i = 0; i < cols.length; i++)
			{
				for (StringTokenizer token = new StringTokenizer(cols[i], "/"); token.hasMoreTokens(); root.addContent(element.setText(data)))
				{
					data = new String(datum.dbif.rs.getString(token.nextToken()).getBytes("UTF-8"), "UTF-8");
					element = new Element(token.nextToken());
				}

			}

		}
	}

	public void Xmlwrite()
	{
		try
		{
			Format format = Format.getCompactFormat();
			format.setEncoding("UTF-8");
			format.setIndent("  ");
			XMLOutputter XMLOut = new XMLOutputter(format);
			XMLOut.output(doc, new FileOutputStream(file));
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}
